package pers.cz.testNg;

import cn.hutool.core.date.DateUtil;
import pers.cz.compilation.DynamicCompilationUtil;
import pers.cz.compilation.TypeFactory;
import pers.cz.mapper.AutoTestCaseParamsCodeMapper;
import pers.cz.model.AutoTestCaseParamsCode;
import pers.cz.properties.CompilationProperties;
import pers.cz.spring.SpringContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Type;
import java.util.*;

/**
 * 处理参数为代码的任务，
 * @program: PostGirl-panent
 * @description: CodeMethodJudgeImpl
 * @author: Cheng Zhi
 * @create: 2021-11-25 15:17
 **/
@Component
public class CodeMethodJudgeImpl implements IMethodJudge{

    private String jarPath; // 从配置文件中获取编译依赖的jar包位置

    private String saveClassPath; // 从配置文件中获取保存临时产生class文件位置。

    @Autowired
    CompilationProperties compilationProperties;

    @Override
    public List<String[]> splitParam(Integer paramId, String params) {

        saveClassPath = compilationProperties.getSaveclasspath();
        jarPath = compilationProperties.getJarpath();
        List<String[]> resultList = new ArrayList<>();
        String[] stringArray = new String[1]; // 初始化定义为1，如果需要再扩容
        // 1、查询数据库
        AutoTestCaseParamsCode autoTestCaseParamsCode = SpringContextUtil.getBean(AutoTestCaseParamsCodeMapper.class).selectByPrimaryKey(paramId);
        // 2、获取代码相关参数
        String codeType = autoTestCaseParamsCode.getCodeType();
        if ("Java".equals(codeType)) {

            String className = autoTestCaseParamsCode.getClassName();
            String methodName = autoTestCaseParamsCode.getMethodName();
            String code = autoTestCaseParamsCode.getCode();
            String[] split = className.split("\\.");
            String runClassName = split[split.length-1];
            // 1、编译
            DynamicCompilationUtil dynamicCompilationUtil = new DynamicCompilationUtil();
            try {
                // 编译文件
                String compilateionResult = dynamicCompilationUtil.compileJava(code, jarPath, saveClassPath, runClassName.trim());

                if ("success".equals(compilateionResult)) {
                    // 反射执行
                    Map<Type, Object> typeObjectMap = dynamicCompilationUtil.loadAndRunClass(saveClassPath, methodName.trim(), className.trim());
                    // 执行完毕解析结果并返回
                    TypeFactory typeFactory = new TypeFactory();

                    for (Type type : typeObjectMap.keySet()) {

                        if ("java.util.Date".equals(type.getTypeName())) {
                            Date date = (Date) typeObjectMap.get(type);
                            // 如果是日期类型需要转换为string的人能看的懂的样子
                            String dateString = DateUtil.formatDateTime(date);
                            stringArray[0] = dateString;
                        }

                        if ("java.lang.Integer".equals(type.getTypeName()) ||
                                "java.lang.Long".equals(type.getTypeName()) ||
                                "java.lang.Double".equals(type.getTypeName()) ||
                                "java.lang.String".equals(type.getTypeName()) ) {

                            // 如果是常见单字符类型则直接强转为String
                            String string = String.valueOf(typeObjectMap.get(type));
                            stringArray[0] = string;
                        }

                        if ("java.lang.List".equals(type.getTypeName())) {
                            int i = 0;
                            List list = (List) typeObjectMap.get(type);
                            // 先对数组扩容
                            stringArray = Arrays.copyOf(stringArray, list.size());
                            for (Object string : list) {
                                stringArray[i] = String.valueOf(string);
                                i++;
                            }
                        }
                    }

                    resultList.add(stringArray);
                }

                return resultList;

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return null;
    }


    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(DateUtil.formatDateTime(date));

        List list = new ArrayList();
        list.add(123);
        list.add(123);
        list.add(123);
        list.add(123);

        List<String> stringList = list;
        for (Object s : stringList) {
            System.out.println(String.valueOf(s));
        }
        Float dou = new Float(123123.4d);
        String douString = String.valueOf(dou);
        System.out.println(douString);
    }
}
